using PathologicalGames;
using System;
using System.Collections.Generic;
using UnityEngine;

public class SpawnEnemy : MonoBehaviour
{
	public tk2dClippedSprite ExpOfPlayer;

	private int EnemyEndless = 1;

	private int countEnemyofTurn;

	private int countTurnEndless;

	private int Enemy;

	private int countWave;

	private int randomEnemy;

	private float tangMau = 1f;

	private int a;

	private int increaseExp;

	private int crrStage;

	public bool checkWin;

	private Vector3 PosInstance;

	public GamePlayController _GamePlay;

	public PlayerController Player;

	private SpawnPool pool;

	private int IndexEnemy;

	private List<int> listTam = new List<int>();

	private void Start()
	{
		pool = base.gameObject.GetComponent<SpawnPool>();
		crrStage = GameManager.stage;
		if (GameManager.levelPlayer < 90)
		{
			ExpOfPlayer.ClipRect = new Rect(ExpOfPlayer.ClipRect.x + (float)GameManager.ExpEnemyDie / (float)GameManager.ExpMaxlevelPlayer[GameManager.levelPlayer - 1], 0f, 1f, 1f);
		}
		if (crrStage < 61)
		{
			checkIndexEnemy();
			if (GameManager.Tutorial > 1)
			{
				Invoke("InstanceEnemy", 2f);
			}
			if (crrStage <= 10)
			{
				increaseExp = 10;
				randomEnemy = 3;
			}
			else if (crrStage <= 20)
			{
				increaseExp = 15;
				randomEnemy = 4;
			}
			else if (crrStage <= 30)
			{
				increaseExp = 20;
				randomEnemy = 3;
			}
			else if (crrStage <= 40)
			{
				increaseExp = 40;
				randomEnemy = 5;
			}
			else if (crrStage <= 50)
			{
				increaseExp = 70;
				randomEnemy = 5;
			}
			else
			{
				increaseExp = 100;
				randomEnemy = 6;
			}
		}
		if (crrStage == 61)
		{
			Invoke("InstanceEnemyEndless", 2f);
		}
		if (crrStage % 10 == 0)
		{
			SinhConBoss();
		}
	}

	private void checkIndexEnemy()
	{
		listTam.Clear();
		for (int i = 0; i < GameManager.Sinh[crrStage].Length; i++)
		{
			for (int j = 0; j < GameManager.Sinh[crrStage][i].Length; j++)
			{
				IndexEnemy = GameManager.Sinh[crrStage][i][j];
				if (!listTam.Contains(IndexEnemy))
				{
					GameObject prefab = (GameObject)Resources.Load("Enemy/" + IndexEnemy.ToString());
					GameObject gameObject = pool.Spawn(prefab, new Vector3(0f, -2.05f, 0f), Quaternion.identity).gameObject;
					pool.Despawn(gameObject.transform);
					listTam.Add(IndexEnemy);
				}
			}
		}
	}

	private void Update()
	{
		if (GameManager.stage < 61 && (checkListEnemyDie() || GameManager.isBossDeath) && !checkWin && countWave == GameManager.Sinh[GameManager.stage - 1].Length)
		{
			CheckWinStage();
		}
	}

	private void CheckWinStage()
	{
		checkWin = true;
		GameManager.isWin = true;
		Time.timeScale = 0.5f;
		_GamePlay.Win();
	}

	private void InstanceEnemyEndless()
	{
		Invoke("InstanceEnemyEndless", 6.5f);
		if (checkListEnemyDie())
		{
			SinhConEndless();
		}
	}

	public void InstanceEnemy()
	{
		if (countWave < GameManager.Sinh[GameManager.stage - 1].Length && !checkWin)
		{
			Invoke("InstanceEnemy", 6.5f);
			if (checkListEnemyDie())
			{
				SinhCon();
			}
		}
	}

	private bool checkListEnemyDie()
	{
		bool flag = false;
		if (GameManager.AllEnemy.Count == 0)
		{
			return true;
		}
		return false;
	}

	private void SinhCon()
	{
		Enemy = GameManager.Sinh[GameManager.stage - 1][countWave][countEnemyofTurn];
		if (Enemy == 27)
		{
			PosInstance = new Vector3(18f, -2f, 0f);
		}
		else if (Enemy == 29)
		{
			new Vector3(UnityEngine.Random.Range(-5, 19), 0f, 0f);
		}
		else if (Enemy > 25)
		{
			PosInstance = new Vector3(UnityEngine.Random.Range(-5, 19), -1f, 0f);
		}
		else
		{
			PosInstance = new Vector3(UnityEngine.Random.Range(-5, 19), -1.8f, 0f);
		}
		GameObject gameObject = pool.Spawn(Resources.Load("Enemy/" + Enemy) as GameObject, PosInstance, Quaternion.identity).gameObject;
		gameObject.gameObject.GetComponent<EnemyController>().EnemyKilled += OnDeath;
		GameManager.AllEnemy.Add(gameObject);
		countEnemyofTurn++;
		if (countEnemyofTurn < GameManager.Sinh[GameManager.stage - 1][countWave].Length)
		{
			Invoke("SinhCon", 0.2f);
			return;
		}
		countWave++;
		countEnemyofTurn = 0;
	}

	private void SinhConEndless()
	{
		countTurnEndless++;
		Enemy = UnityEngine.Random.Range(0, 25);
		PosInstance = new Vector3(UnityEngine.Random.Range(-5, 19), -1.8f, 0f);
		GameObject gameObject = (GameObject)UnityEngine.Object.Instantiate(Resources.Load("Enemy/" + Enemy), PosInstance, Quaternion.identity);
		gameObject.gameObject.GetComponent<EnemyController>().EnemyKilled += OnDeath;
		gameObject.gameObject.GetComponent<EnemyController>().HealthMaxEnemy *= tangMau;
		GameManager.AllEnemy.Add(gameObject);
		if (countTurnEndless < EnemyEndless)
		{
			Invoke("SinhConEndless", 0.2f);
		}
		else
		{
			if (EnemyEndless < 30)
			{
				EnemyEndless += 2;
			}
			countTurnEndless = 0;
		}
		if (GameManager.numberKill >= 10 && GameManager.numberKill < 50)
		{
			if (a == 0)
			{
				InstanceBossEndless(25, 1.5f);
				a++;
			}
			increaseExp = 25;
			tangMau = 1.5f;
		}
		else if (GameManager.numberKill >= 50 && GameManager.numberKill < 100)
		{
			if (a == 1)
			{
				InstanceBossEndless(26, 1.5f);
				a++;
			}
			increaseExp = 35;
			tangMau = 2.5f;
		}
		else if (GameManager.numberKill >= 100 && GameManager.numberKill < 200)
		{
			if (a == 2)
			{
				InstanceBossEndless(27, 1.5f);
				a++;
			}
			increaseExp = 45;
			tangMau = 4f;
		}
		else if (GameManager.numberKill >= 200 && GameManager.numberKill < 500)
		{
			if (a == 3)
			{
				InstanceBossEndless(28, 2f);
				a++;
			}
			increaseExp = 60;
			tangMau = 6f;
		}
		else if (GameManager.numberKill >= 500 && GameManager.numberKill < 1000)
		{
			if (a == 4)
			{
				InstanceBossEndless(29, 2f);
				a++;
			}
			increaseExp = 80;
			tangMau = 10f;
		}
		else if (GameManager.numberKill >= 1000)
		{
			if (a == 5)
			{
				InstanceBossEndless(30, 2f);
				GameManager.numberKill = 0;
				a = 0;
			}
			increaseExp = 100;
			tangMau = 20f;
		}
	}

	private void InstanceBossEndless(int idBoss, float hs)
	{
		if (Enemy == 27)
		{
			PosInstance = new Vector3(18f, -2f, 0f);
		}
		else if (Enemy == 29)
		{
			new Vector3(UnityEngine.Random.Range(-5, 19), 0f, 0f);
		}
		else
		{
			PosInstance = new Vector3(UnityEngine.Random.Range(-5, 19), -1f, 0f);
		}
		GameObject gameObject = UnityEngine.Object.Instantiate(Resources.Load("Enemy/" + idBoss), PosInstance, Quaternion.identity) as GameObject;
		gameObject.gameObject.GetComponent<EnemyController>().EnemyKilled += OnDeath;
		gameObject.GetComponent<EnemyController>().HealthMaxEnemy *= hs;
		GameManager.AllEnemy.Add(gameObject);
	}

	private void SinhConBoss()
	{
		if (!Player.GetComponent<PlayerController>().PlayerisDeath)
		{
			if (!checkWin && countWave == GameManager.Sinh[GameManager.stage - 1].Length)
			{
				SinhconBoss1();
			}
			Invoke("SinhConBoss", 10f);
		}
	}

	private void SinhconBoss1()
	{
		countTurnEndless++;
		Enemy = UnityEngine.Random.Range(0, 25);
		GameObject gameObject = (GameObject)UnityEngine.Object.Instantiate(Resources.Load("Enemy/" + Enemy), new Vector3(UnityEngine.Random.Range(-5, 19), -1.8f, 0f), Quaternion.identity);
		gameObject.gameObject.GetComponent<EnemyController>().EnemyKilled += OnDeath;
		GameManager.AllEnemy.Add(gameObject);
		if (countTurnEndless < randomEnemy)
		{
			Invoke("SinhconBoss1", 0.2f);
		}
		else
		{
			countTurnEndless = 0;
		}
	}

	public void OnDeath(object sender, EventArgs e)
	{
		GameManager.numberKill++;
		GameManager.Key_numberkill++;
		if (GameManager.levelPlayer < 90)
		{
			GameManager.ExpEnemyDie += increaseExp;
			ExpOfPlayer.ClipRect = new Rect(ExpOfPlayer.ClipRect.x + (float)increaseExp / (float)GameManager.ExpMaxlevelPlayer[GameManager.levelPlayer - 1], 0f, 1f, 1f);
			if (GameManager.ExpEnemyDie >= GameManager.ExpMaxlevelPlayer[GameManager.levelPlayer - 1])
			{
				GameManager.ExpEnemyDie = 0;
				ExpOfPlayer.ClipRect = new Rect(-1f, 0f, 1f, 1f);
				GameManager.levelPlayer++;
				Player.countDownBlood();
			}
		}
	}
}
